package com.douing.ad.dsp.bidding.rtb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.douing.ad.dsp.bidding.dto.AdRequestDto;
import com.douing.ad.dsp.bidding.dto.AdResponseDto;
import com.douing.ad.dsp.bidding.dto.Admt;
import com.douing.ad.dsp.bidding.dto.AdsDto;
import com.douing.ad.dsp.bidding.dto.AppRequestDto;
import com.douing.ad.dsp.bidding.dto.AppResponseDto;
import com.douing.ad.dsp.bidding.dto.Bid;
import com.douing.ad.dsp.bidding.dto.MatchedCreativeDto;
import com.douing.ad.dsp.bidding.dto.SeatBid;
import com.douing.ad.dsp.bidding.tj.utils.NodeData;

public class GenerateResponse {
    public static AdResponseDto BuildResponse(AdRequestDto request, MatchedCreativeDto finalCreative, NodeData nodeData,Map<String,NodeData> nodeDataMap) {
        AdResponseDto response = new AdResponseDto();
        response.setBidding(false);
        response.setRequestId(request.getRequestId());
        // response.setCategory(request.getCategory());
        response.setCookieId(request.getUser().getUserId());
        response.setVersion(request.getVersion());

        response.setReqTime(request.getReqTime());
        AdsDto ad = new AdsDto();
        if (finalCreative == null) {
            return response;
        }
        response.setMediaId(finalCreative.getMediaId());
        response.setAreaId(finalCreative.getAreaId());
        response.setCategory(new Long[] {finalCreative.getMatchedcatid()});
        ad.setAdId(request.getImp().getAdSlotId());
        ad.setImpId(request.getImp().getImpId());
        ad.setCreativeId(String.valueOf(finalCreative.getId()));
        ad.setAdDomain("");
        ad.setPrice(finalCreative.getBidPrice());
        ad.setMaxPrice(finalCreative.getMaxPrice());
        ad.setAdmt(finalCreative.getAdmt());
        ad.setAdUrl(finalCreative.getUrl());
        ad.setDestinationUrl(finalCreative.getTargetUrl());
        ad.setAdWidth(request.getImp().getBanner().getWidth());
        ad.setAdHeight(request.getImp().getBanner().getHeight());
        ad.setAdvertiserId(finalCreative.getCreateUserId());
        ad.setCreativeTradeId(finalCreative.getCreativeTradeId());
        response.setAds(new AdsDto[] {ad});
        response.setBidding(true);
        Common.WriteLog("bidding finish and success! requestid is " + request.getRequestId() + ";advertiserid is "
                + ad.getAdvertiserId() + ";bidprice is " + finalCreative.getBidPrice());
        // TODO
        NodeData nd = nodeDataMap.get(finalCreative.getId()+"");
        nd.setExitNum(1); 
        
        return response;
    }

    @SuppressWarnings("null")
    public static AppResponseDto BuildAppResponse(AppRequestDto request, List<MatchedCreativeDto> finalCreatives, NodeData nodeData,Map<String,NodeData> nodeDataMap) {
        AppResponseDto response = new AppResponseDto();
        response.setBidding(false);
        response.setRequestId(request.getRequestId());
        // response.setCookieId(request.getUser().getUserId());

        response.setReqTime(request.getReqTime());
        if (finalCreatives == null && finalCreatives.size() > 0) {
            return response;
        }

        boolean hasarea = false;
        List<Bid> ads = new ArrayList<Bid>();
        for (MatchedCreativeDto finalCreative : finalCreatives) {
            if (!hasarea) {
                response.setAreaId(finalCreative.getAreaId());
                response.setAppCat(String.valueOf(finalCreative.getMatchedcatid()));
                hasarea = true;
            }
            
            response.setAppId(finalCreative.getMediaId());
            
            Bid ad = new Bid();
            ad.setImpId(finalCreative.getImpid());
            ad.setCid(String.valueOf(finalCreative.getId()));
            ad.setCrid(String.valueOf(finalCreative.getId()));
            ad.setAdDomain("");
            ad.setPrice(finalCreative.getBidPrice());
            ad.setMaxPrice(finalCreative.getMaxPrice());
            ad.setAdmt(1);
            ad.setAdUrl(finalCreative.getUrl());
            ad.setClickUrl(finalCreative.getTargetUrl());
            ad.setAdWidth(Float.valueOf(finalCreative.getWidth()).intValue());
            ad.setAdHeight(Float.valueOf(finalCreative.getHeight()).intValue());
            ad.setAdvertiserId(finalCreative.getCreateUserId());
            ad.setAdid(String.valueOf(finalCreative.getActivityId()));
            ad.setAdct(1);
            ads.add(ad);
        }
        SeatBid seatBid = new SeatBid();

        if (ads.size() > 0) {
            Bid[] bids = new Bid[ads.size()];
            ads.toArray(bids);
            seatBid.setBids(bids);
            response.setBidding(true);
            Common.WriteLog("app bidding finish and success! requestid is " + request.getRequestId());
            // TODO
            for(Bid ad :ads){
                NodeData nd = nodeDataMap.get(ad.getCid());
                nd.setExitNum(1);
            }
        }else{
            Common.WriteLog("app bidding finish and failed! requestid is " + request.getRequestId());
        }
        response.setSeatbids(new SeatBid[] {seatBid});
        return response;
    }
}
